package com.ruoyi.manage.controller;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;

import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.manage.domain.User;
import com.ruoyi.manage.service.IUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import java.util.Arrays;
import com.ruoyi.common.core.domain.R;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.utils.StringUtils;

/**
 * 博客用户管理Controller
 * 
 * @author kl
 * @since 2025-06-07
 */
@Tag(name = "博客用户管理")
@RestController
@RequestMapping("/manage/user")
@RequiredArgsConstructor
public class UserController extends BaseController
{

    private final IUserService userService;

    /**
     * 查询博客用户管理列表
     */
    @Operation(summary = "查询博客用户管理列表")
    @PreAuthorize("@ss.hasPermi('manage:user:list')")
    @GetMapping("/list")
    public R<Page<User>> list(@RequestParam(defaultValue = "1") Integer pageNum,
                                      @RequestParam(defaultValue = "10") Integer pageSize,
                                      User user)
    {
        Page<User> page = new Page<>(pageNum, pageSize);
        Page<User> userPage = userService.lambdaQuery()
                    .like(StringUtils.isNotBlank(user.getUsername()), User::getUsername, user.getUsername())
                    .between(StringUtils.isNotBlank((String) user.getParams().get("beginCreateTime"))
                            && StringUtils.isNotBlank((String) user.getParams().get("endCreateTime")),
                        User::getCreateTime,
                        user.getParams().get("beginCreateTime"),
                        user.getParams().get("endCreateTime"))
                    .page(page);
        return R.ok(userPage);
    }

    /**
     * 导出博客用户管理列表
     */
    @Operation(summary = "导出博客用户管理列表")
    @PreAuthorize("@ss.hasPermi('manage:user:export')")
    @Log(title = "博客用户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, User user)
    {
            List<User> list = userService.lambdaQuery()
                    .like(StringUtils.isNotBlank(user.getUsername()), User::getUsername, user.getUsername())
                    .between(StringUtils.isNotBlank((String) user.getParams().get("beginCreateTime"))
                            && StringUtils.isNotBlank((String) user.getParams().get("endCreateTime")),
                        User::getCreateTime,
                        user.getParams().get("beginCreateTime"),
                        user.getParams().get("endCreateTime"))
                    .list();
        ExcelUtil<User> util = new ExcelUtil<>(User.class);
        util.exportExcel(response, list, "博客用户管理数据");
    }

    /**
     * 获取博客用户管理详细信息
     */
    @Operation(summary = "获取博客用户管理详细信息")
    @PreAuthorize("@ss.hasPermi('manage:user:query')")
    @GetMapping(value = "/{id}")
    public R<User> getInfo(@PathVariable("id") Long id)
    {
        return R.ok(userService.getById(id));
    }

    /**
     * 新增博客用户管理
     */
    @Operation(summary = "新增博客用户管理")
    @PreAuthorize("@ss.hasPermi('manage:user:add')")
    @Log(title = "博客用户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R<String> add(@RequestBody User user)
    {
        if (userService.save(user)){
            return R.ok("新增成功！");
        }
        return R.fail("新增失败！");
    }

    /**
     * 修改博客用户管理
     */
    @Operation(summary = "修改博客用户管理")
    @PreAuthorize("@ss.hasPermi('manage:user:edit')")
    @Log(title = "博客用户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<String> edit(@RequestBody User user)
    {
        if (userService.updateById(user)){
        return R.ok("修改成功！");
    }
        return R.fail("修改失败！");
    }

    /**
     * 删除博客用户管理
     */
    @Operation(summary = "删除博客用户管理")
    @PreAuthorize("@ss.hasPermi('manage:user:remove')")
    @Log(title = "博客用户管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public R<String> remove(@PathVariable Long[] ids)
    {
        if (userService.removeBatchByIds(Arrays.asList(ids))){
            return R.ok("删除成功！");
        }
        return R.fail("删除失败！");
    }
}
